#!/usr/bin/python3.2
# -*- coding: utf-8 -*-
import urllib.request  
import urllib.parse  
import http.cookiejar
import re
from collections import deque
import socket
socket.setdefaulttimeout(5)


log_url = "http://www.renren.com/PLogin.do"
cj=http.cookiejar.CookieJar()
opener=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
opener.addheaders=[('User-agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)')]
urllib.request.install_opener(opener) #Installing an opener is only necessary if you want urlopen to use that opener
print('输入账号:')
email="1296109800@qq.com" #input("email=")
password="zxcvbnmz" #input("password=")
params={"email":email, "password":password}#, "origURL":self.origURL, "domain":self.domain}  
params=urllib.parse.urlencode(params)  
params=params.encode('utf-8')
opener.open(log_url,params)
guidpage=opener.open(log_url,params).geturl()
print(guidpage)
data = opener.open(guidpage).read().decode('utf-8')
#ruid=guidpage.split('/')[-1]
#print(ruid)
REG = re.compile('ruid:"(.+?)"')
res = REG.findall(data)
#print(res[0])


#print(friends)
def get_friends():
    page = 0
    friends = set()
    friends |= {res[0]}
    FREG = re.compile('<a href="http://www\.renren\.com/profile\.do\?id=(.+?)"')
    nextREG = re.compile('下一页')
    for i in range(1):#爬好友层数
        friends_f = friends.copy()
        for f in friends_f:
            while True:
                listurl = 'http://friend.renren.com/GetFriendList.do?curpage='+str(page)+'&id=' + f
                data = opener.open(listurl).read().decode('utf-8')
                fs = FREG.findall(data)
                #print(fs)
                friends |= set(fs)
                r = nextREG.findall(data)
                if len(r) == 0:
                    break
                page += 1
    return friends


PLREG = re.compile('"albumId":"(.+?)"')
PREG = re.compile('"url":"(.+?)"')


def Schedule(a,b,c):
    '''''
    a:已经下载的数据块
    b:数据块的大小
    c:远程文件的大小
   '''
    per = 100.0 * a * b / c
    if per > 100 :
        per = 100
    print('%.2f%% ' % per,end="")
def download_album(user_id,album_id):
    album_url = 'http://photo.renren.com/photo/'+user_id+'/album-'+album_id+'/v7'
    
    plist = opener.open(album_url).read().decode('utf-8')
    photos = PREG.findall(plist)
    print(album_url+"("+str(len(photos))+")")
    for p in photos:
        path = 'd:\\picture2' + '\\' + p.split('/')[-1]
        print(".", end="")
        p = p.replace('\\','')
        try:
            #urllib.request.urlretrieve(p,path,Schedule)
            urllib.request.urlretrieve(p,path)
        except:
            print("\nError:"+ p+"\n")
    print("")
def get_album_by_userid(user_id):
    photolist_url = 'http://photo.renren.com/photo/'+ user_id + '/albumlist/v7?showAll=1#'
    data = opener.open(photolist_url).read().decode('utf-8')
    aids = PLREG.findall(data)
    albumIds = set(aids)
    return albumIds
    

#friends = get_friends()
friends = ["505229331","505229331","235349690","287577859","321861186","250376526","327672413"
,"581802044","239594561","467301128","273443223","827952987","288215523","252041655","511743131"
,"303547971","84198281","272803192","563719791","345104033","353130430","327672413","528901101"
,"301119327","301119327","223355695","457480256","321861186","312680277","433050781","306888767"
,"377807905","545620318","582599552","354252339","512201515","375920272","282250769","348238623"

]
print("friends:"+str(len(friends)))
for f in friends:
    albumIds = get_album_by_userid(f)
    print("friend:"+"http://www.renren.com/profile.do?id="+f+" albums:"+str(len(albumIds)))
    for i in albumIds:
        download_album(f,i)
